﻿/*
创建人：longb
日期：2022-07-11
描述：材料申领单js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    pageType: getParam("type"), //0：创建 1：确认 2：审核
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSNotProductMaterialApplyBillHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        if(page.pageType=="0"){$('#create').show();$('#ConfirmAuditRetrun').hide();$('.Editor').show();$('.NoEditor').hide();}else{$('#create').hide();$('.NoEditor').show();$('.Editor').hide(); }
        if(page.pageType=="1"){$('#Retrun').show();$('#Confirm').show();$('#Audit').hide();}
        if(page.pageType=="2"){$('#Retrun').show();$('#Confirm').hide();$('#Audit').show();}
    },
    //页面事件绑定
    bind: function () {
        var Initid = 0;
        var data = [{ id: 0, text: '待提交' }, { id: 1, text: '待确认' },  { id: 2, text: '待审核' }, { id: 3, text: '已审核'}, { id: 4, text: '已退回'}];
        if(page.pageType=="1"){Initid=1;data = [{ id: 1, text: '待确认' },  { id: 2, text: '待审核' }, { id: 3, text: '已审核'}, { id: 4, text: '已退回'}];}
        if(page.pageType=="2"){Initid=2;data = [{ id: 2, text: '待审核' }, { id: 3, text: '已审核'}, { id: 4, text: '已退回'}];}
        $('#Status').combobox({
            data: data,
            valueField: 'id',
            textField: 'text',
            onSelect: function (record) {
            }
        });
        $('#Status').combobox('setValue', Initid);
        $('#AuxMaterial').newselectCombox({},{},{url:page.ajaxUrl,action:"GetNPMaterial",index:1});
        $('#TOGETHERFLAGDIV').newselectCombox({
            search: function (obj) { },
            cancel: function (obj) { if(obj.index==2){$('#TOGETHERFLAG').val('');page.GetWorkFlowData('');}else{$('#AUXMATERIALID').val('');}},
            selected: function (val, text,record,obj){
                if(obj.index==2){
                    $('#TOGETHERFLAG').val(text);
                    page.GetWorkFlowData(val);
                }else{
                  $('#AUXMATERIALID').val(val);
                }
                $('#txtCombox'+obj.index).textbox('setValue',text);
                $('#divComboxTxt'+obj.index).show();
                $('#divComboxSelect'+obj.index).hide(); 
            }
        },{},{url:page.ajaxUrl,action:"GetContainerNo",index:2});

        page.post({action:"GetUom"}, function (res) {
            $('#UOMID').combobox({
                data: res.DBTable,
                valueField: 'ID',
                textField: 'NAME',
                onSelect: function (record) {
                }
            });
        });
       
        //申领单信息
        $("#AuxMaterialApplyTable").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ZSNPMATERIALAPPLYBILLID', width: 120, checkbox: true },
                 { field: 'ZSNPMATERIALAPPLYBILLNO', title: '申领单号', width: 130 },
                 { field: 'MATERIALCODE', title: '物料编码', width: 120 },
                 { field: 'MATERIALDESC', title: '物料描述', width: 120 },
		         { field: 'APPLYQTY', title: '申领数量', width: 80 }, //--
                 { field: 'UOMNAME', title: '单位', width: 90 }, 
                 { field: 'TOGETHERFLAG', title: '任务单号', width: 90 }, 
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 90 }, 
                 { field: 'PURPOSE', title: '用途', width: 90 },  
                 { field: 'APPLYNAME', title: '申领人', width: 90 },  
                 { field: 'APPLYDATE', title: '申领时间', width: 90 },  
                 { field: 'STATUS', title: '状态', width: 80,  },
                 { field: 'CONFIRMNMAE', title: '确认人', width: 80,  },
                 { field: 'CONFIRMDATE', title: '确认时间', width: 130 },
                 { field: 'AUDITNAME', title: '审核人', width: 80,},
                 { field: 'AUDITDATE', title: '审核时间', width: 130 },
                 { field: 'RETURNREASON', title: '退回原因', width: 130,formatter: function(value,row,index){
                    if(value!=null&&row.AUDITRETURNREASON!=null){ return value+","+row.AUDITRETURNREASON;}
                    else if(value!=null&&row.AUDITRETURNREASON==null){ return value;}
                    else if(value==null&&row.AUDITRETURNREASON!=null){ return row.AUDITRETURNREASON;}else{return"";}
                 } }
	           ]],
            onSelect: function (index, row) {
                $('#WORKFLOWSTEPID').combobox({
                    data: [{WORKFLOWSTEPID:row.WORKFLOWSTEPID,OPDESC:row.WORKFLOWSTEPNAME}],
                    valueField: 'WORKFLOWSTEPID',
                    textField: 'OPDESC'
                });
                row["MATERIALCODE"] = row.MATERIALCODE;
                $('#EditorForm').form('load', row);
                if(!$('#btnComboxSearch1').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox1').combobox('disable');
                    $('#btnComboxSearch1').linkbutton('disable');
                }
                if(!$('#btnComboxSearch2').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox2').combobox('disable');
                    $('#btnComboxSearch2').linkbutton('disable');
                }
                $('#txtCombox1').textbox('setValue',row.MATERIALCODE);
                $('#txtCombox2').textbox('setValue',row.TOGETHERFLAG);
               
                $("#AuxMaterialApplyTable").datagrid("uncheckAll").datagrid("checkRow",index);//单选行，去掉同时勾选
            },
            onCheckAll:function(rows){
                $("#AuxMaterialApplyTable").datagrid("clearSelections");
                 $('#EditorForm').form("clear");
            },
            onCheck:function(rowIndex,rowData){
                var checkRows= $("#AuxMaterialApplyTable").datagrid("getChecked");
                var selectRow= $("#AuxMaterialApplyTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#AuxMaterialApplyTable").datagrid("clearSelections");
                     $('#EditorForm').form("clear");
                }
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetNPMaterialApplyBill();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetNPMaterialApplyBill();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetNPMaterialApplyBill();
        page.CreateNPMaterialApplyBill();
    },
    GetWorkFlowData: function (workflowid) {
        var param = {
            action: "GetWorkFlowData",
            type:"",
            workflowid: workflowid
        };
        page.post(param, function (res) {
            if (res.Result) {
                $('#WORKFLOWSTEPID').combobox({
                    data: res.DBTable,
                    valueField: 'WORKFLOWSTEPID',
                    textField: 'OPDESC'
                });
            }
        });
    },
    //查询材料申领单信息
    GetNPMaterialApplyBill: function () {
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetNPMaterialApplyBill",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#AuxMaterialApplyTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    //保存材料申领单信息
    SaveNPMaterialApplyBill: function () {
        var row = $('#AuxMaterialApplyTable').datagrid('getSelected');
        if (row != null && row.STATUS != "待提交"&&row.STATUS != "已退回") { displayMessage(row.STATUS + "的提交单，不能保存", false); return; }
        var frdata = $('#EditorForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "SaveNPMaterialApplyBill",
            data: JSON.stringify([data])
        };
        if (data.AUXMATERIALID == ""||$('#txtCombox1').textbox('getValue')=="") { displayMessage("物料名称编码不能为空，请选择", false); return; }
        if (data.APPLYQTY == "") { displayMessage("申领数量不能为空，请输入", false); return; }
        if (data.UOMID == "") { displayMessage("数量单位不能为空，请选择", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetNPMaterialApplyBill();
                page.CreateNPMaterialApplyBill();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //新建材料申领单信息
    CreateNPMaterialApplyBill: function () {
        $('#EditorForm').form("clear");
        $('#txtCombox1').combobox('enable');
        $('#btnComboxSearch1').linkbutton('enable');
        $('#txtCombox2').combobox('enable');
        $('#btnComboxSearch2').linkbutton('enable');
        $('#AuxMaterialApplyTable').datagrid('unselectAll');
        clearMessage();
    },
    //申领单--提交
    SubmitNPMaterialApplyBill: function () {
        page.NPMaterialApplyBill("SubmitNPMaterialApplyBill");
    },
    //申领单--确认
    ConfirmNPMaterialApplyBill: function () {
        page.NPMaterialApplyBill("ConfirmNPMaterialApplyBill");
    },
    //申领单--审核
    AuditNPMaterialApplyBill:function(){
        page.NPMaterialApplyBill("AuditNPMaterialApplyBill");
    },
    //申领单--退回
    RetrunNPMaterialApplyBill:function(){
        page.NPMaterialApplyBill("RetrunNPMaterialApplyBill");
    },
    //申领单--删除
    DaleteNPMaterialApplyBill:function(){
        page.NPMaterialApplyBill("DaleteNPMaterialApplyBill");
    },
    NPMaterialApplyBill: function (action) {
        var ids = [];
        var rows = $('#AuxMaterialApplyTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSNPMATERIALAPPLYBILLID);
        });
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        if(action=="SubmitNPMaterialApplyBill"&&rows[0].STATUS!="待提交"){displayMessage("所选提交单"+rows[0].STATUS+",不能再提交",false);return;}
        if(action=="ConfirmNPMaterialApplyBill"&&rows[0].STATUS!="待确认"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行确认",false);return;}
        if(action=="AuditNPMaterialApplyBill"&&rows[0].STATUS!="待审核"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行审核",false);return;}
        //if(action=="RetrunAuxMaterialApplyBill"&&(rows[0].STATUS!="待审核"&&page.pageType==2))){displayMessage("所选提交单"+rows[0].STATUS+",不能进行退回",false);return;}
        if(page.pageType==1&&action=="RetrunNPMaterialApplyBill"){
            if(rows[0].STATUS!="待确认"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行退回",false);return;}
        }
         if(page.pageType==2&&action=="RetrunNPMaterialApplyBill"){
            if(rows[0].STATUS!="待审核"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行退回",false);return;}
        }
        if(action=="DaleteNPMaterialApplyBill"&&rows[0].STATUS!="待提交"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行删除",false);return;}
        var param = {
            action: action,
            type:page.pageType,
            ids: ids.join(','),
            data:JSON.stringify(rows)
        };
        if (action=="RetrunNPMaterialApplyBill"&&$('#RETURNREASON').textbox("getValue")=="") { displayMessage("请输入退回原因，再进行操作", false); return; }else{
            param.RETURNREASON=$('#RETURNREASON').textbox("getValue");
        }

        page.post(param, function (res) {
            if (res.Result) {
                page.GetNPMaterialApplyBill();
                page.CreateNPMaterialApplyBill();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});